package com.mm.controller.note;

import cn.hutool.core.util.StrUtil;
import com.mm.base.exception.NoteException;
import com.mm.domain.common.DCornQuartz;
import com.mm.serivce.quartz.CornQuartzService;
import com.mm.util.quartz.SchedulerJobUtil;
import com.mm.util.result.DResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronExpression;
import org.quartz.SchedulerException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 描述: 定时器
 *
 * @author: yanglin
 * @Date: 2021-07-29-16:37
 * @Version: 1.0
 */
@Api(value = "/task/v1/", tags = {"描述: 定时器"})
@Slf4j
@RestController
@RequestMapping("/task/v1/")
public class QuartzController {

    private final CornQuartzService quartzService;

    private final SchedulerJobUtil schedulerJobUtil;

    public QuartzController(CornQuartzService quartzService, SchedulerJobUtil schedulerJobUtil) {
        this.quartzService = quartzService;
        this.schedulerJobUtil = schedulerJobUtil;
    }

    /**
     * 定时器当月任务记录
     *
     * @param day
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "day", value = "", required = true)
    })
    @ApiOperation(value = "定时器当月任务记录", notes = "定时器当月任务记录", httpMethod = "GET")
    @GetMapping("query")
    public DResult getQuartzList(@RequestParam String day) {
        List<DCornQuartz> quartzList = quartzService.queryQuartzList(day);
        return DResult.of(quartzList);
    }

    /**
     * 定时器多参数查询
     *
     * @param map
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Map<String, Object>", dataTypeClass = Map.class, name = "map", value = "", required = true)
    })
    @ApiOperation(value = "定时器多参数查询", notes = "定时器多参数查询", httpMethod = "POST")
    @PostMapping("queryMap")
    public DResult getQuartzList(@RequestBody Map<String, Object> map) {
        List<DCornQuartz> quartzs = quartzService.queryQuartzList(map);
        return DResult.of(quartzs);
    }

    /**
     * 记录明细
     *
     * @param cornId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "cornId", value = "", required = true)
    })
    @ApiOperation(value = "记录明细", notes = "记录明细", httpMethod = "GET")
    @GetMapping("queryOne")
    public DResult getQuartzById(@RequestParam String cornId) {
        Long qId = 0L;
        if (StrUtil.isNotBlank(cornId) && !"undefined".equals(cornId)) {
            qId = Long.parseLong(cornId);
        }
        DCornQuartz quartz = quartzService.queryQuartzById(qId);
        return DResult.of(quartz);
    }

    /**
     * 定时器任务编辑
     *
     * @param quartz
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DCornQuartz", dataTypeClass = DCornQuartz.class, name = "quartz", value = "", required = true)
    })
    @ApiOperation(value = "定时器任务编辑", notes = "定时器任务编辑", httpMethod = "POST")
    @Transactional
    @PostMapping("saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DCornQuartz quartz) throws SchedulerException {
        boolean flag = false;
        // 验证Corn表达式
        if (!CronExpression.isValidExpression(quartz.getQuartzTime())) {
            return DResult.failed("N0005", "Corn表达式错误");
        } else {
            if (quartz.getCornId() < 0) {
                // 新增
                flag = quartzService.save(quartz);
            } else if (quartz.getCornId() > 0) {
                // 更新
                flag = quartzService.updateById(quartz);
            } else {
                return DResult.failed("N0004", "任务ID为空");
            }
            DCornQuartz newQuartz = quartzService.getById(quartz.getCornId());
            schedulerJobUtil.addQuartz(newQuartz);
        }
        return DResult.of(flag);
    }

    /**
     * 定时器任务删除
     *
     * @param cornId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "cornId", value = "", required = true)
    })
    @ApiOperation(value = "定时器任务删除", notes = "定时器任务删除", httpMethod = "GET")
    @Transactional
    @GetMapping("remove")
    public DResult remove(@RequestParam Long cornId) throws SchedulerException {
        boolean flag = false;
        if (Objects.nonNull(cornId)) {
            DCornQuartz delQuartz = quartzService.getById(cornId);
            schedulerJobUtil.removeQuartz(delQuartz);
            flag = quartzService.remove(cornId);

        } else {
            throw new NoteException("N0004", "任务ID为空或为负数");

        }
        return DResult.of(flag);
    }


}
